﻿//-----------------------------------------------------------------------
// <copyright file="KinectMultipointWindow.cs" company="xBrainLab">
//     Copyright (c) xBrainLab. All rights reserved.
// </copyright>
// <author>Sebastien Warin</author>
//-----------------------------------------------------------------------
namespace KinectTouchDevice
{
    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Imaging;

    public class KinectMultipointWindow : Window
    {
        private const int DST_TOUCH_DOWN = 1200;

        private static Uri cursor_hand_drag_uri = new Uri("pack://application:,,,/KinectTouchDevice;component/Images/cursor_drag_hand.png");
        private static Uri cursor_hand_uri = new Uri("pack://application:,,,/KinectTouchDevice;component/Images/cursor_hand.png");

        public static KinectHands kinect;

        private Canvas kinectHandsCanvas = null;
        private ContentPresenter contentPresenter = null;

        static KinectMultipointWindow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(KinectMultipointWindow), new FrameworkPropertyMetadata(typeof(KinectMultipointWindow)));
        }

        public KinectMultipointWindow()
        {
            this.Closing += (s, e) => kinect.Dispose();
            this.Loaded += new RoutedEventHandler(KinectWindow_Loaded);
        }

        void KinectWindow_Loaded(object sender, RoutedEventArgs e)
        {
            kinectHandsCanvas = this.GetControlDecendantByName<Canvas>("kinectHandsCanvas");
            contentPresenter = this.GetControlDecendantByName<ContentPresenter>("contentPresenter");
            this.LoadKinect();
        }

        private void LoadKinect()
        {
            Debug.WriteLine("Connect to Kinect...");
            kinect = new KinectHands();
            Debug.WriteLine("Attaching Kinect events...");
            kinect.HandPointUpdated += new EventHandler<HandPointEventArgs>(kinect_HandPointUpdated);
            kinect.HandPointDetroy += new EventHandler<NITE.IdEventArgs>(kinect_HandPointDetroy);
        }

        void kinect_HandPointDetroy(object sender, NITE.IdEventArgs e)
        {
            if (kinectHandsCanvas != null)
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    Image image = kinectHandsCanvas.GetControlsDecendant<Image>().Where(img => img.Tag.Equals(e.ID.ToString())).FirstOrDefault();
                    if (image != null)
                    {
                        kinectHandsCanvas.Children.Remove(image);
                    }
                }), null);
            }
        }

        private void kinect_HandPointUpdated(object sender, HandPointEventArgs e)
        {
            e.Position = new OpenNI.Point3D(
                (float)(this.ActualWidth / kinect.Width) * e.Position.X,
                (float)(this.ActualHeight / kinect.Height) * e.Position.Y,
                e.Position.Z);

            //Debug.WriteLine("HandPointUpdated - Position : {0} x {1} - Id: {2}", Math.Round(e.Position.X, 0), Math.Round(e.Position.Y, 0), e.Id);
            Dispatcher.BeginInvoke(new Action(() =>
            {
                KinectMultiTouchDevice.MouseMove(e);

                Image image = kinectHandsCanvas.GetControlsDecendant<Image>().Where(img => img.Tag.Equals(e.ID.ToString())).FirstOrDefault();
                if (image != null)
                {
                    this.Title = "Distance : " + e.Position.Z;

                    Canvas.SetLeft(image, e.Position.X);
                    Canvas.SetTop(image, e.Position.Y);

                    if (e.Position.Z < DST_TOUCH_DOWN && image.Source.ToString().Contains("cursor_hand.png"))
                    {
                        KinectMultiTouchDevice.MouseDown(this, e);

                        image.Source = new BitmapImage(cursor_hand_drag_uri);
                    }
                    else if (e.Position.Z > DST_TOUCH_DOWN && image.Source.ToString().Contains("cursor_drag_hand.png"))
                    {
                        KinectMultiTouchDevice.MouseUp(e);

                        image.Source = new BitmapImage(cursor_hand_uri);
                    }
                }
                else
                {
                    Image img = new Image() { Height = 48, Width = 48, Source = new BitmapImage(cursor_hand_uri), Tag = e.ID.ToString() };
                    kinectHandsCanvas.Children.Add(img);
                    Canvas.SetLeft(img, e.Position.X);
                    Canvas.SetTop(img, e.Position.Y);
                }
            }), null);
        }
    }
}
